package session.stateless;

import entity.Employee;
import entity.Product;
import entity.ProductionBatch;
import entity.ProductionScheduleDay;
import entity.WarehouseZone;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class ProductionBatchSessionBean implements ProductionBatchSessionBeanLocal{

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void addProductionBatch(Integer quantity, String remark,
            String productionRepId, String productId, Long productionScheduleDayId) {
        Timestamp ts = new Timestamp(Calendar.getInstance().getTime().getTime());
        Employee employee = entityManager.find(Employee.class, productionRepId);
        Product product = entityManager.find(Product.class, productId);
        ProductionScheduleDay psd = entityManager.find(ProductionScheduleDay.class, productionScheduleDayId);
        ProductionBatch pb = new ProductionBatch(ts, quantity,0, remark, employee, null, product, psd);
        entityManager.persist(pb);
        entityManager.flush();


    }

    @Override
    public ArrayList<ProductionBatch> getProductionBatches(Boolean hasZone) {
        Query query = entityManager.createQuery("SELECT u FROM ProductionBatch u");
        ArrayList<ProductionBatch> pbs = new ArrayList<ProductionBatch>();
        for (Object o : query.getResultList()) {
            ProductionBatch pb = (ProductionBatch) o;
            if (hasZone) {
                if (pb.getWarehouseZone() != null) {
                    pbs.add(pb);
                }
            } else {
                if (pb.getWarehouseZone() == null) {
                    pbs.add(pb);
                }
            }
        }
        return pbs;


    }

    @Override
    public ArrayList <ProductionBatch> getProductionBatches() {
        Query query = entityManager.createQuery("SELECT u FROM ProductionBatch u");
        ArrayList<ProductionBatch> pbs = new ArrayList<ProductionBatch>();
        for (Object o : query.getResultList()) {
            ProductionBatch pb = (ProductionBatch) o;
            pbs.add(pb);
        }

        return pbs;
    }
    
    @Override
    public void updateProductionBatchZone (Long productionBatchId, Long warehouseZoneId)
    {
        ProductionBatch pb = entityManager.find(ProductionBatch.class, productionBatchId);
       WarehouseZone whz = entityManager.find(WarehouseZone.class,warehouseZoneId);
       pb.setWarehouseZone(whz);
       entityManager.persist(pb);
       entityManager.flush();
        
        
        
    }
    
    @Override
    public void updateProductionBatch(ProductionBatch pb) {
        ProductionBatch temp = entityManager.find(ProductionBatch.class, pb.getId());
        
        temp.setUtilizedQuantity(pb.getUtilizedQuantity());
        
        entityManager.persist(temp);
        entityManager.flush();
    
        
    }
    
    @Override
     public void updateProductionBatch (Long productionBatchId, Integer utilizedQuantity)
     {
         ProductionBatch pb = entityManager.find (ProductionBatch.class, productionBatchId);
         pb.setUtilizedQuantity(utilizedQuantity);
         entityManager.persist (pb);
         entityManager.flush ();
         
     }
}
